package com.handpay.zztong.hp.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.log.HPLog;

public class HPSelectBankView extends LinearLayout
{
	private ImageView	mBankLogo;
	private ImageView	mArrow;
	private TextView	mBankName;
	private PopupWindow	mPopup;
	// 记录被选择的位置
	private int			mSelected	= -1;
	private List<Bank>	mBanks		= new ArrayList<Bank>(2);
	
	public HPSelectBankView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init();
	}
	
	public HPSelectBankView(Context context)
	{
		super(context);
		init();
	}
	
	private void init()
	{
		this.setOrientation(LinearLayout.HORIZONTAL);
		this.mBankLogo = new ImageView(this.getContext());
		this.mBankLogo.setClickable(false);
		this.mBankLogo.setVisibility(View.GONE);
		this.mArrow = new ImageView(this.getContext());
		this.mArrow.setClickable(false);
		this.mArrow.setImageResource(R.drawable.pull_down);
		this.mBankName = new TextView(this.getContext());
		// this.mBankName.setHint(R.string.hint_input_bank_type);
		this.mBankName.setClickable(false);
		this.mBankName.setTextColor(Color.BLACK);
		this.mBankName
				.setTextSize(TypedValue.COMPLEX_UNIT_PX, this.getResources()
						.getDimensionPixelSize(R.dimen.normal_text_size));
		this.mBankName.setHintTextColor(this.getResources().getColor(
				R.color.bank_name_hint_color));
		this.setGravity(Gravity.CENTER_VERTICAL);
		this.setBackgroundResource(R.drawable.block_bg);
		// this.set
		this.addView(mBankLogo, new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(0,
				LayoutParams.WRAP_CONTENT, 1);
		ll.leftMargin = (int) this.getResources().getDimension(
				R.dimen.interval_controls);
		this.addView(mBankName, ll);
		ImageView ivDiv = new ImageView(this.getContext());
		ivDiv.setBackgroundColor(this.getResources().getColor(
				R.color.bank_list_view_divider));
		this.addView(ivDiv, new LinearLayout.LayoutParams(2,
				LayoutParams.MATCH_PARENT));
		LinearLayout.LayoutParams arrowLayout = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
		int margin = (int) this.getResources().getDimension(
				R.dimen.interval_controls);
		arrowLayout.rightMargin = margin;
		arrowLayout.leftMargin = margin;
		this.addView(mArrow, arrowLayout);
		this.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				showBankSelect(v);
			}
		});
		mPopup = new PopupWindow(this.getContext());
		mPopup.setHeight(LayoutParams.WRAP_CONTENT);
		mPopup.setBackgroundDrawable(this.getResources().getDrawable(
				R.drawable.pull_down_box));
		ListView lv = new ListView(this.getContext());
		lv.setDivider(this.getResources().getDrawable(
				R.color.bank_list_view_divider));
		lv.setDividerHeight(1);
		lv.setAdapter(new BankAdapter(this.getContext()));
		lv.setOnItemClickListener(new OnItemClickListener()
		{
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3)
			{
				if (null != mBanks && arg2 >= 0 && mBanks.size() > arg2)
				{
					mSelected = arg2;
					mBankLogo.setVisibility(View.VISIBLE);
					mBankLogo.setImageResource(mBanks.get(arg2).logoResId);
					mBankName.setText(mBanks.get(arg2).name);
				}
				mPopup.dismiss();
			}
		});
		mPopup.setContentView(lv);
		mPopup.setFocusable(true);
		mPopup.setTouchable(true);
	}
	
	/**
	 * 如果popupwindow已经显示，不作处理。如果没有显示，显示在view下面
	 * 
	 * @param view
	 *            popupwindow要显示的地方。
	 */
	private void showBankSelect(View view)
	{
		if (!mPopup.isShowing())
		{
			mPopup.setWidth(view.getWidth());
			mPopup.showAsDropDown(view);
		}
	}
	
	/**
	 * 在返回的时候，确保这个popup window要dismiss
	 * 
	 * @return
	 */
	public boolean dismissPopup()
	{
		if (mPopup.isShowing())
		{
			mPopup.dismiss();
			return true;
		}
		return false;
	}
	
	/**
	 * 设置提示消息
	 * 
	 * @param resId
	 */
	public void setBankHints(int resId)
	{
		this.mBankName.setHint(resId);
	}
	
	/**
	 * 返回选中的位置
	 * 
	 * @return null 没有选择
	 */
	public String getSelectedBankId()
	{
		if (mSelected >= 0 && mSelected < mBanks.size())
		{
			return mBanks.get(mSelected).bankId;
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * 添加可以选择的银行
	 */
	public void addBanks(String bankId, String bankName, int resourceId)
	{
		mBanks.add(new Bank(bankId, bankName, resourceId));
	}
	
	private class Bank
	{
		String	bankId;
		String	name;
		int		logoResId;
		
		public Bank(String id, String nameBank, int resId)
		{
			bankId = id;
			name = nameBank;
			logoResId = resId;
		}
	}
	
	private class BankAdapter extends BaseAdapter
	{
		private LayoutInflater	li;
		
		public BankAdapter(Context context)
		{
			li = LayoutInflater.from(context);
		}
		
		@Override
		public int getCount()
		{
			return mBanks.size();
		}
		
		@Override
		public Object getItem(int position)
		{
			return mBanks.get(position);
		}
		
		@Override
		public long getItemId(int position)
		{
			return position;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			// 暂且不使用viewHolder
			HPLog.i("HPSelectBank", "get postion " + position);
			View container = null;
			if (null == convertView)
			{
				convertView = li.inflate(R.layout.bank_item, null);
			}
			container = convertView;
			((ImageView) container.findViewById(R.id.ivBankLogo))
					.setImageResource(mBanks.get(position).logoResId);
			((TextView) container.findViewById(R.id.tvBankName)).setText(mBanks
					.get(position).name);
			if (mSelected == position)
			{
				container.findViewById(R.id.ivChecked)
						.setVisibility(View.VISIBLE);
			}
			else
			{
				container.findViewById(R.id.ivChecked)
						.setVisibility(View.GONE);
			}
			return container;
		}
		
	}
	
}
